package com.inwk.valoepi.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.internal.*;

import com.inwk.valoepi.managers.PageManager;

public class AuthorizedPage extends Page {
	
	protected String cartXPathLocator = "//div[@id='pnlTopCart']";
	
	@FindBy(xpath = "//div[@class='header-nav-item']//img[@alt='logo']")
	private WebElement productsListLink;
	
	@FindBy(xpath = "//div[@class='header-nav-item']//div[@class='icon icn-portal']")
	private WebElement mainPageLink;
	
	@FindBy(xpath = "//div[@class='header-nav-item']//input[@id='productSearchTerms']")
	private WebElement searchField;
	
	@FindBy(xpath = "//div[@class='header-nav-item']//span[@class='icn-search']")
	private WebElement searchButton;
	
	@FindBy(xpath = "//div[@class='header-nav-item']//div[@class='icon icn-profile']")
	private WebElement userMenu;
	
	@FindBy(xpath = "//div[@class='header-nav-item']//div[@class='icon icn-cart']")
	private WebElement cartMenu;
	
	public AuthorizedPage(PageManager pm) {
		super(pm);
		// TODO Auto-generated constructor stub
	}
	
	public ShopPage goToShopPage() {
		productsListLink.click();
		return pm.shopPage;
	}
	
	public MainPage goToMainPage() {
		mainPageLink.click();
		return pm.mainPage;
	}
	
	public ShopPage search(String searchString) {
		searchField.clear();
		searchField.sendKeys(searchString);
		searchButton.click();
		return pm.shopPage;
	}
	
	public ShopPage clearSearchField() {
		searchField.clear();
		searchButton.click();
		return pm.shopPage;
	}
	
	public UserProfilePage goToUserProfile() {
		((Locatable)userMenu).getCoordinates().onPage();
		openUserMenu();
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='header-nav-item']//a[@id='userProfileLink']"))).click();
		return pm.profilePage;
	}
	
	public OrderHistoryPage goToOrderHistory() {
		openUserMenu();
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='header-nav-item']//a[@id='orderHistoryLink']"))).click();
		return pm.orderHistoryPage;
	}
	
	public StartPage doLogout() {
		((Locatable)userMenu).getCoordinates().onPage();
		//((Locatable)userMenu).getLocationOnScreenOnceScrolledIntoView();
		openUserMenu();
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='header-nav-item']//a[@id='userLogoutLink']"))).click();
		return pm.startPage;
	}

	public void clearCart() {
		WebElement cartDiv;
		webDriver.get(webDriver.getCurrentUrl());
		try { 
			cartDiv = (new WebDriverWait(webDriver, 5)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(cartXPathLocator)));
		} catch(TimeoutException e) {
			cartDiv = null;
		}
		if(cartDiv != null) {
			wait.until(ExpectedConditions.presenceOfElementLocated(By.id("productSearchTerms"))).sendKeys("");
			new Actions(webDriver)
			.moveToElement(cartDiv)
			.perform();

			wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(cartXPathLocator + "//div[@class='minicartRow'][last()]")));
			wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(cartXPathLocator + "//*[contains(@class,'minicartClear btn')]"))).click();
			wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='info-mess big clear-cart']/a[@class='btn btn-primary small']"))).click();
			
			wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(cartXPathLocator)));
		}
	}
	
	public int getProductsNumberInCart() {
		WebElement cartDiv;
		int number = 0;
		try { 
			cartDiv = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(cartXPathLocator)));
		} catch(TimeoutException e) {
			cartDiv = null;
		}
		if(cartDiv != null) {
			number = Integer.parseInt(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(cartXPathLocator + "//*[@id='headerCartCount']"))).getText());
		}
		return number;
	}

	@Override
	public boolean isCurrentPage() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private void openUserMenu() {
		Point menuCoordinates = ((Locatable)userMenu).getCoordinates().onPage();
		new Actions(webDriver)
		.moveToElement(userMenu)
		.perform();
	}

}
